// 论坛主题
// by indraw 2013.03.01
package bbs

import (
	//"fmt"
    "strconv"
    "utils/web"
    "utils/util"
    "time"

)

/**
 * 发帖页面
 *
 * author: indraw
 * date: 2013-03-01
 */
func ThreadPost(ctx *web.Context) string {

	//初始化
	initApp(ctx)

	SessUserID,_ := session.Get("userid")
	if SessUserID == ""{
		return tpl.Success("请您登录后发表文章")

	}

	//执行操作
    if ctx.Params["act"] == "do"{
		//写入主题
		thread := newThread()
		thread.setFid(ctx.Params["fid"])
		thread.setSubject(ctx.Params["subject"])
		thread.setSortid(ctx.Params["sortid"])
		thread.setIcon(ctx.Params["icon"])
		SessUserID,_ := session.Get("userid")
		SessUserName,_ := session.Get("username")
		thread.setUserid(SessUserID)
		thread.setUsername(SessUserName)
		if err := thread.add(); err != nil {
			return tpl.Error(err.Error())
		}
		
		//写入帖子
		post := newPost()
		post.setTid(thread.getTid())
		post.setFid(ctx.Params["fid"])
		post.setContent(util.GetInputContent(ctx.Params["elm1"]))
		post.setSubject(ctx.Params["subject"])
		post.setUsesig(ctx.Params["usesig"])

        
		post.setUserid(SessUserID)
		post.setUsername(SessUserName)
		post.Useip = util.GetUserIP(ctx.Request)
		post.First = 1
		if err := post.add(); err != nil {
			return tpl.Error(err.Error())
		}
        //修改板块最后发表和主题数量
        forum := newForum()
        forum.setFid(ctx.Params["fid"])
        forum.Lastpost["userid"] = SessUserID
        forum.Lastpost["username"] = SessUserName
        forum.Lastpost["subject"] = ctx.Params["subject"]
        forum.Lastpost["tid"] = thread.getTid()
        forum.Lastpost["time"] = strconv.FormatInt(time.Now().Unix(),10)
		if err := forum.updateLastpost(0); err != nil {
			return tpl.Error(err.Error())
		}
        
        //
        user := newUser()
        user.setUserid(SessUserID)
        user.Lastpost = ctx.Params["subject"]
		if err := user.updatePost(0); err != nil {
			return tpl.Error(err.Error())
		}
        
        //更新主题帖子数量
        //thread.
        
        //记录发表时间
        session.Set("post",strconv.FormatInt(time.Now().Unix(),10) )
		
		//return tpl.Success("发帖成功:" + ctx.Params["subject"])
        tpl.Assign("Fid", ctx.Params["fid"])
        tpl.Assign("Tid", thread.getTid())
        tpl.Assign("Message", "成功发表主题！")

        return tpl.Display("success_post.html")

    }
	
	//执行操作
	//获取板块信息
    forum := newForum()
    forum.setFid(ctx.Params["fid"])
	if err := forum.get(); err != nil {
		return tpl.Error(err.Error())
	}

    forumRoot := newForum()
    forumRoot.setFid(strconv.Itoa(forum.Fup))
	if err := forumRoot.get(); err != nil {
		return tpl.Error(err.Error())
	}
	//获取分类
	sort := newSort() 
	sortTop := sort.top()
	
	//获取分组
	groupid,_ := session.Get("groupid")
	groupid2,_ := strconv.Atoi(groupid)
	group := SysGroup[groupid2]
	
    tpl.Assign("Forum", forum.ForumData)
    tpl.Assign("ForumRoot", forumRoot.ForumData)
    tpl.Assign("SortTop", sortTop)
    tpl.Assign("Group", group)
    
    tpl.Assign("Uploadtype", cfg["upload"]["filetype"])

    return tpl.Display("thread_post.html")
    

}


/**
 * 主题查看页面
 *
 * author: indraw
 * date: 2013-03-01
 */
func ThreadRead(ctx *web.Context,tid string) string {
	
	//初始化
	initApp(ctx)
	
	//执行操作
	//获取主题信息
	thread := newThread()
	thread.setTid(tid)
	if err := thread.get(); err != nil {
		return tpl.Error(err.Error())
	}
    thread.updateViews()
	
	//获取板块信息
    forum := newForum()
    forum.setFid(thread.getFid())
	if err := forum.get(); err != nil {
		return tpl.Error(err.Error())
	}

    forumRoot := newForum()
    forumRoot.setFid(strconv.Itoa(forum.Fup))
	if err := forumRoot.get(); err != nil {
		return tpl.Error(err.Error())
	}
	
	//获取所有帖子
    post := newPost()
    post.setTid(tid)
    num := post.count()
    var p int
    p, _ = strconv.Atoi(ctx.Params["p"])
    page := util.NewPageMaker(num, p, tid)
    postList := post.listMore(page)

	
	
	
	//页面显示
    tpl.Assign("Forum", forum.ForumData)
    tpl.Assign("ForumRoot", forumRoot.ForumData)
    tpl.Assign("PostNum", page)
    //tpl.Assign("PostID", page.Top)

    tpl.Assign("Thread", thread.ThreadData)
    tpl.Assign("PostList", postList)

	//显示分组
	SessUserGroup,_ := session.Get("groupid")
	groupid,_ := strconv.Atoi(SessUserGroup)
	//fmt.Println(SysGroup[groupid].Allowhtml)
    tpl.Assign("UserGroup", SysGroup[groupid])
    

    //判断帖子是否显示
    tpl.Assign("hideContent", 0)

    //显示title
    tpl.Assign("BbsTitle", cfg["bbs"]["title"] + "-" + thread.Subject)

    return tpl.Display("thread_read.html")

}
